﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class EditPurchaseDetails : BasePage
    {
        PurchaseOrderDetails purchaseDetails;
        int poDetailsId;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[KEY] == null)
            {
                CloseDlg(0);
                return;
            }

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            purchaseDetails = (PurchaseOrderDetails)Session[Request.QueryString[KEY]];
            string oldFilter = purchaseDetails.Filter;

            poDetailsId = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode != ADD_MODE)
                purchaseDetails.Filter = PurchaseOrderDetails.ColumnNames.ID + "=" + poDetailsId.ToString();

            if (!IsPostBack && purchaseDetails.RowCount > 0 && mode != ADD_MODE)
                LoadInfo();

            purchaseDetails.Filter = oldFilter;
        }

        private void LoadInfo()
        {
            if (!purchaseDetails.IsColumnNull("Description"))
                editPODetails.ProductName = purchaseDetails.Description;
            if (!purchaseDetails.IsColumnNull("Quantity"))
                editPODetails.Quantity = purchaseDetails.Quantity;
            if (!purchaseDetails.IsColumnNull("GST"))
                editPODetails.GST = purchaseDetails.GST;
            if (!purchaseDetails.IsColumnNull("TotalCost"))
                editPODetails.Total = purchaseDetails.TotalCost;
            if (!purchaseDetails.IsColumnNull("UnitPrice"))
                editPODetails.UnitPrice = purchaseDetails.UnitPrice;
            if (!purchaseDetails.IsColumnNull("ProductID"))
                editPODetails.ProductID = purchaseDetails.ProductID;
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            if (purchaseDetails != null)
            {
                if (Save())
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
            }
        }

        private bool Save()
        {
            string oldFilter = purchaseDetails.Filter;
            if (mode == ADD_MODE)
            {
                purchaseDetails.AddNew();
                purchaseDetails.BranchID = Branches.CurrentBranchID;
                purchaseDetails.Deleted = false;
            }
            else
            {
                purchaseDetails.Filter = Categories.ColumnNames.ID + "=" + poDetailsId;
            }

            if (!purchaseDetails.IsColumnNull("IMEI") && purchaseDetails.IMEI != "")
            {
                ((Popup)Master).ShowErrorMessage("Can not edit a IMEI product");
                purchaseDetails.Filter = oldFilter;
                return false;
            }

            Decimal oldGST = purchaseDetails.GST;

            purchaseDetails.Quantity = editPODetails.Quantity;
            purchaseDetails.UnitPrice = editPODetails.UnitPrice;
            purchaseDetails.TotalCost = Math.Round(purchaseDetails.Quantity * purchaseDetails.UnitPrice, 2);
            purchaseDetails.GST = oldGST == 0 ? 0 : Math.Round(purchaseDetails.TotalCost * SysVars.GST / (SysVars.GST + 100), 2);

            purchaseDetails.LastModifiedByUser = SessionUser.ID;
            purchaseDetails.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);

            //invoiceDetails.Save();
            purchaseDetails.Filter = oldFilter;

            Products.UpdateCost(purchaseDetails.ProductID, purchaseDetails.UnitPrice);
            ((Popup)Master).ShowMessage("Saved");
            return true;
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (purchaseDetails != null)
            {
                if (Save())
                    CloseDlg(1);
            }
        }
    }
}
